package com.xs.designs.adapter.demo2;

import cn.hutool.core.util.StrUtil;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserAdapter {

    private static final Logger logger = LoggerFactory.getLogger(UserAdapter.class);

    @Autowired
    private RemoteUserService remoteUserApi;

    public UserDTO getUserDetail(String username) {
        // 参数校验
        if(StrUtil.isBlank(username)) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        long t1 = System.currentTimeMillis();
        UserDTO2 userDTO2 = remoteUserApi.getUserDetail(username);
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userDTO2,userDTO);
        // UserDTO2 ---> UserDTO
        long t2 = System.currentTimeMillis();
        // 打印慢接口日志
        if(t2 - t1 > 3000L) {
            logger.warn("调用外部接口耗时过长，cost:[{}]ms", t2 - t1);
        }
        if (userDTO2==null) {
            throw new RuntimeException("用户：" + username + " 不存在");
        }
        return userDTO;
    }
}